package leetcode.code0043;

/**
 * 第一版, 未简化代码 同 Solution
 *
 * @author CaoYu
 * @date 2021年10月3日
 * @description
 */
public class Solution1 extends Solution43 {

	@Override
	public String multiply(String num1, String num2) {
		if (num1.equals("0") || num2.equals("0"))
			return "0";
		char[] n1 = num1.toCharArray();
		char[] n2 = num2.toCharArray();
		int[] mult = new int[n1.length + n2.length];
		int len = mult.length;
		int pos = 1;
		for (int i = n1.length - 1; i >= 0; i--) {
			int start = len - pos;
			int carry = 0;
			for (int j = n2.length - 1; j >= 0; j--) {
				int m = (n1[i] - '0') * (n2[j] - '0') + carry;
				mult[start] += m % 10;
				carry = m / 10 + mult[start] / 10;
				mult[start--] %= 10;
			}
			mult[start] += carry;
			pos++;
		}
		StringBuffer ans = new StringBuffer();
		boolean flag = false;
		for (int n : mult) {
			if (n != 0) {
				flag = true;
			}
			if (flag) {
				ans.append(n);
			}
		}
		return ans.toString();
	}

	public static void main(String[] args) {
		Solution1 so = new Solution1();
		so.debug1();
		so.debug2();
//		System.out.println(9999 * 9999);
	}

}
